package com.czkt.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czkt.pojo.SysUser;
import com.czkt.pojo.vo.SysUserVo;
import com.czkt.service.ISysRoleService;
import com.czkt.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {

    @Autowired
    SysUserService sysUserService;


    @Autowired
    ISysRoleService sysRoleService;

    @Resource
    StringRedisTemplate stringRedisTemplate;



    @RequestMapping("/doLogin")
    public Object doLogin(String usrName, String usrPassword,Boolean remeberMe, Model model, HttpSession session) {
        SysUserVo sysUser = sysUserService.login(usrName, usrPassword);
        if (sysUser == null) {
            model.addAttribute("message", "账号或密码错误！");
            return "login";
        } else {
            System.out.println("remeberMe"+remeberMe);
            if(remeberMe!=null){//如果记住账号  存入redis中
                stringRedisTemplate.opsForValue().set("loginUser", JSON.toJSONString(sysUser));
            }
            session.setAttribute("loginUser", sysUser);
            return "main";
        }

    }

    @RequestMapping("/logout")
    public Object logout(HttpSession session) {
        session.removeAttribute("loginUser");
        return "login";
    }


    @RequestMapping("/user/list")
    public Object getList(@RequestParam(defaultValue = "1") Integer pageIndex,
                          @RequestParam(defaultValue = "2") Integer pageSize,
                          @RequestParam(defaultValue = "0") Long roleId,
                          SysUserVo sysUserVo,

                          Model model) {
        IPage<SysUserVo> page = new Page<SysUserVo>(pageIndex, pageSize);
        sysUserVo.setUsrRoleId(roleId);
//        SysUserVo sysUserVo = new SysUserVo();
        model.addAttribute("userPager", sysUserService.getPage(page, sysUserVo));
        model.addAttribute("roles", sysRoleService.list());
        model.addAttribute("usrName", sysUserVo.getUsrName());
        model.addAttribute("roleId", sysUserVo.getUsrRoleId());
        return "user/list";
    }

    @GetMapping("/list")
    @ResponseBody
    public Object index() {
        return sysUserService.list();
    }


    /**
     * 定义跳转到新增页的方法
     */
    @RequestMapping("/user/add")
    public Object toAdd(Model model) {
        model.addAttribute("roles", sysRoleService.list());
        return "user/add";
    }


    /**
     * 保存用户信息的方法
     */
    @PostMapping("/user/save")
    public Object saveUser(SysUser sysUser) {
        boolean result = sysUserService.saveOrUpdate(sysUser);
        if (result) {
            return "redirect:/user/list";
        } else if (sysUser.getUsrId() == 0){
            return "redirect:/user/add";
        }else{
            return "redirect:/user/edit?usrId="+sysUser.getUsrId();
        }
    }


    /**
     * 获取用户详细并跳转修改页面
     */
    @RequestMapping("/user/edit")
    public Object toUpdate(long usrId, Model model) {

        model.addAttribute("user", sysUserService.getById(usrId));
        model.addAttribute("roles", sysRoleService.list());
        return "user/edit";
    }


    /**
     * 删除用户信息
     */
    @RequestMapping("/crm/user/del/{usrId}")
    @ResponseBody
    public Object del(@PathVariable() long usrId, Model model) {
        Map resultMap = new HashMap();
        resultMap.put("delResult",sysUserService.removeById(usrId));
        return resultMap;
    }





}
